﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using Startup.Base.Xml;
using Startup.Shell.ModelDesignerPreview;

namespace Startup.Shell.Domain
{
	[XmlInclude(typeof(StartupClass))]
	[XmlInclude(typeof(StartupDomainModelClass))]
	public class StartupProject : StoragedItem
	{
		public string Name { get; set; }
		public ObservableCollection<StartupClass> Classes { get; set; }
		public PreviewContainer MainWindow { get; set; }

		public StartupProject()
		{
			Classes = new ObservableCollection<StartupClass>();
			MainWindow = new PreviewContainer { Preview = new WindowDesignerPreview() };
		}

		public StartupClass DomainClass 
		{
			get { return Classes.FirstOrDefault(x => x is StartupDomainModelClass); }
		}

		internal StartupProperty FindProperty(Guid propertyId)
		{
			StartupProperty property = DomainClass.FindProperty(propertyId);

			if (property != null)
				return property;

			for (int index = 0; index < Classes.Count; index++)
			{
				StartupClass target = Classes[index];

				property = target.FindProperty(propertyId);

				if (property != null)
					return property;
			}

			return null;
		}

		public override string ToString()
		{
			return Name;
		}
	}
}
